package com.example.demo.controller.Test;

import com.example.demo.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;
@RestController
@Slf4j
public class UploadImgController {
    static public LocalDateTime localDateTime;//时间

//本地存储
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件shangcqL{},{},{}",username,age,image);
//        //获取原始文件名 - 1.jpg 123.0.0.jpg
//        String originalFilename = image.getOriginalFilename();
//
//        //构造唯一的文件名(不重复)-uuid
//        int index = originalFilename.lastIndexOf(".");
//        String extname = originalFilename.substring(index);
//        String newFileName = UUID.randomUUID().toString()+extname;
//
//        //将文件存储在服务器的磁盘目录中
//        image.transferTo(new File("D:\\lj\\"+newFileName));
//
//
//        return Result.success();
//    }

    @PostMapping("/upload")//这里的变量名image要与前端对应
    public Result upload(String username, Integer age,MultipartFile image) throws IOException {
        //把文件的内容存储到本地磁盘上
        String originalFilename = image.getOriginalFilename();
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        image.transferTo(new File("D:\\lj\\"+filename));

        return Result.success();
    }
}
